﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

using Microsoft.Office.Interop.OneNote;

using OnPowerShapeAddin.ObjectModel.OneNote2010XMLSchema;

namespace OnPowerShapeAddin
{
    class OneNoteHelper
    {
        private readonly ApplicationClass oneNote;

        public XNamespace XmlNamespace
        {
            get;
            private set;
        }

        public OneNoteHelper(ApplicationClass oneNote)
        {
            this.oneNote = oneNote;
        }
        
        public void GetNamespace()
        {
            string xml;
            oneNote.GetHierarchy(null, HierarchyScope.hsNotebooks, out xml);

            var doc = XDocument.Parse(xml);
            this.XmlNamespace = doc.Root.Name.Namespace;
        }

        public PageUpdateContext CreateUpdateContext(string pageId)
        {
            return new PageUpdateContext(this, pageId);
        }

        public void UpdatePageContent(Page page)
        {
            string pageXml = SerializePage(page);
            oneNote.UpdatePageContent(pageXml, DateTime.MinValue, XMLSchema.xs2010, true);
        }

        public void DeletePageContent(string pageId, string objectId)
        {
            if (string.IsNullOrEmpty(pageId))
                throw new ArgumentNullException("pageId");
            if (string.IsNullOrEmpty(objectId))
                throw new ArgumentNullException("objectId");

            oneNote.DeletePageContent(pageId, objectId);
        }

        public void DeletePageContent(string pageId, PageObject pageObject)
        {
            if (string.IsNullOrEmpty(pageId))
                throw new ArgumentNullException("pageId");
            if (pageObject == null)
                throw new ArgumentNullException("objectId");

            DeletePageContent(pageId, pageObject.objectID);
        }

        public Page GetCurrentPageContent(PageInfo pageInfo)
        {
            return GetPageContent(oneNote.Windows.CurrentWindow.CurrentPageId, pageInfo);
        }

        public Page GetPageContent(string pageId, PageInfo pageInfo)
        {
            if (string.IsNullOrEmpty(pageId))
                throw new ArgumentNullException("pageId");

            string pageXml;

            oneNote.GetPageContent(pageId, out pageXml, pageInfo, XMLSchema.xs2010);

            return DeserializePageXml(pageXml);
        }

        public Page DeserializePageXml(string pageXml)
        {
            XmlSerializer seri = new XmlSerializer(typeof(Page));

            using (StringReader reader = new StringReader(pageXml))
            {
                return (Page)seri.Deserialize(reader);
            }
        }

        public string SerializePage(Page page)
        {
            XmlSerializer seri = new XmlSerializer(typeof(Page));

            using(StringWriter sw = new StringWriter())
	        {
                seri.Serialize(sw, page);

                return sw.ToString();
	        }            
        }
    }
}
